<template>
  <div class="app-container">
    <!-- {{ userStore.permissions }} -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="L/C批次号(先进先出)L250507" prop="materialBatch">
        <el-input
          v-model.trim="queryParams.materialBatch"
          placeholder="请输入L/C批次号(先进先出)L250507"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="物料码" prop="materialCode">
        <el-input
          v-model.trim="queryParams.materialCode"
          placeholder="请输入物料码"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="料箱编号" prop="containerId">
        <el-input
          v-model.trim="queryParams.containerId"
          placeholder="请输入料箱编号"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="ReelID" prop="reelId">
        <el-input
          v-model.trim="queryParams.reelId"
          placeholder="请输入ReelID"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="绑定操作的Port口ID" prop="portId">
        <el-input
          v-model.trim="queryParams.portId"
          placeholder="请输入绑定操作的Port口ID"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="区分待检料和已检料" prop="isQualityCheck">
        <el-input
          v-model.trim="queryParams.isQualityCheck"
          placeholder="请输入区分待检料和已检料"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="需要截料的数量" prop="lockNum">
        <el-input
          v-model.trim="queryParams.lockNum"
          placeholder="请输入需要截料的数量"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="为空表示该物料未被使用，不为空表示物料已被该单号占用" prop="usedByCode">
        <el-input
          v-model.trim="queryParams.usedByCode"
          placeholder="请输入为空表示该物料未被使用，不为空表示物料已被该单号占用"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="厂别(库区划分)" prop="plantCode">
        <el-input
          v-model.trim="queryParams.plantCode"
          placeholder="请输入厂别(库区划分)"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="是否截料" prop="isCutMaterial">
        <el-input
          v-model.trim="queryParams.isCutMaterial"
          placeholder="请输入是否截料"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="乐观锁" prop="version">
        <el-input
          v-model.trim="queryParams.version"
          placeholder="请输入乐观锁"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="入库标签栏位" prop="bin">
        <el-input
          v-model.trim="queryParams.bin"
          placeholder="请输入入库标签栏位"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="是否入库前之前截留" prop="isQualityCut">
        <el-input
          v-model.trim="queryParams.isQualityCut"
          placeholder="请输入是否入库前之前截留"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="用途主体编码" prop="usedByBodyCode">
        <el-input
          v-model.trim="queryParams.usedByBodyCode"
          placeholder="请输入用途主体编码"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:inventory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:inventory:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:inventory:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:inventory:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="downloadTemplate"
        >下载导入模板</el-button>
      </el-col>
      <el-col :span="1.5">
        <MyUpload
          buttonName="导入模板"
          url="/api/inventory/import"
          @success="handleUploadSuccess"
          @warning="handleUploadWarning"
          />
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <TableScrollContainer :topHeight="220">
      <template #default="{ tableHeight }">
        <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange" :height="tableHeight" border>
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="库存主键id" align="center" prop="inventorId" />
          <el-table-column label="L/C批次号(先进先出)L250507" align="center" prop="materialBatch" />
          <el-table-column label="物料码" align="center" prop="materialCode" />
          <el-table-column label="料箱编号" align="center" prop="containerId" />
          <el-table-column label="ReelID" align="center" prop="reelId" />
          <el-table-column label="库存数量" align="center" prop="inventoryQty" />
          <el-table-column label="入库时间" align="center" prop="inboundTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.inboundTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="储格编码" align="center" prop="compartmentCode" />
          <el-table-column label="冻结状态(0:未冻结 1:冻结)" align="center" prop="frozenState" />
          <el-table-column label="备注" align="center" prop="note" />
          <el-table-column label="绑定状态  0未绑定  1绑定料箱 2绑定播种墙 3绑定货架" align="center" prop="status" />
          <el-table-column label="D/C(生产日期)D250507" align="center" prop="productionDate" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="模穴" align="center" prop="cavityNo" />
          <el-table-column label="版本" align="center" prop="revision" />
          <el-table-column label="创建人" align="center" prop="createdBy" />
          <el-table-column label="更新人" align="center" prop="updateBy" />
          <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="法人信息" align="center" prop="corporation" />
          <el-table-column label="物料名称" align="center" prop="materialName" />
          <el-table-column label="是否为急料" align="center" prop="isUrgent" />
          <el-table-column label="供应厂商编码" align="center" prop="supplierCode" />
          <el-table-column label="物料收料时间" align="center" prop="receivingTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.receivingTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="物料生产时间" align="center" prop="dateCode" />
          <el-table-column label="物料等级" align="center" prop="materialGrade" />
          <el-table-column label="物料有效期" align="center" prop="validityPeriod" />
          <el-table-column label="物料有效期单位" align="center" prop="validityUnit" />
          <el-table-column label="物料暴露有效期" align="center" prop="exposureTime" />
          <el-table-column label="物料暴露有效期单位" align="center" prop="exposureUnit" />
          <el-table-column label="收料人员工号编码" align="center" prop="recipientCode" />
          <el-table-column label="绑定操作的Port口ID" align="center" prop="portId" />
          <el-table-column label="区分待检料和已检料" align="center" prop="isQualityCheck" />
          <el-table-column label="已检料状态" align="center" prop="qualityCheckType" />
          <el-table-column label="需要截料的数量" align="center" prop="lockNum" />
          <el-table-column label="借料状态：NULL-正常" align="center" prop="borrowStatus" />
          <el-table-column label="为空表示该物料未被使用，不为空表示物料已被该单号占用" align="center" prop="usedByCode" />
          <el-table-column label="仓别" align="center" prop="warehouseType" />
          <el-table-column label="厂别(库区划分)" align="center" prop="plantCode" />
          <el-table-column label="是否截料" align="center" prop="isCutMaterial" />
          <el-table-column label="乐观锁" align="center" prop="version" />
          <el-table-column label="入库标签栏位" align="center" prop="bin" />
          <el-table-column label="是否入库前之前截留" align="center" prop="isQualityCut" />
          <el-table-column label="用途主体编码" align="center" prop="usedByBodyCode" />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding
            fixed-width"
            fixed="right"
            width="200"
          >
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:inventory:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:inventory:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <pagination
          v-show="total>0"
          :total="total"
          :page-sizes="[10, 30, 50, 100]"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />

      </template>
    </TableScrollContainer>
    <!-- 添加或修改库存对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" :close-on-click-modal="false">
      <el-form ref="inventoryRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="L/C批次号(先进先出)L250507" prop="materialBatch">
          <el-input v-model="form.materialBatch" placeholder="请输入L/C批次号(先进先出)L250507" />
        </el-form-item>
        <el-form-item label="物料码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料码" />
        </el-form-item>
        <el-form-item label="料箱编号" prop="containerId">
          <el-input v-model="form.containerId" placeholder="请输入料箱编号" />
        </el-form-item>
        <el-form-item label="ReelID" prop="reelId">
          <el-input v-model="form.reelId" placeholder="请输入ReelID" />
        </el-form-item>
        <el-form-item label="库存数量" prop="inventoryQty">
          <el-input v-model="form.inventoryQty" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="入库时间" prop="inboundTime">
          <el-date-picker clearable
            v-model="form.inboundTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择入库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="储格编码" prop="compartmentCode">
          <el-input v-model="form.compartmentCode" placeholder="请输入储格编码" />
        </el-form-item>
        <el-form-item label="冻结状态(0:未冻结 1:冻结)" prop="frozenState">
          <el-input v-model="form.frozenState" placeholder="请输入冻结状态(0:未冻结 1:冻结)" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="D/C(生产日期)D250507" prop="productionDate">
          <el-date-picker clearable
            v-model="form.productionDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择D/C(生产日期)D250507">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="模穴" prop="cavityNo">
          <el-input v-model="form.cavityNo" placeholder="请输入模穴" />
        </el-form-item>
        <el-form-item label="版本" prop="revision">
          <el-input v-model="form.revision" placeholder="请输入版本" />
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="法人信息" prop="corporation">
          <el-input v-model="form.corporation" placeholder="请输入法人信息" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="是否为急料" prop="isUrgent">
          <el-input v-model="form.isUrgent" placeholder="请输入是否为急料" />
        </el-form-item>
        <el-form-item label="供应厂商编码" prop="supplierCode">
          <el-input v-model="form.supplierCode" placeholder="请输入供应厂商编码" />
        </el-form-item>
        <el-form-item label="物料收料时间" prop="receivingTime">
          <el-date-picker clearable
            v-model="form.receivingTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择物料收料时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="物料生产时间" prop="dateCode">
          <el-input v-model="form.dateCode" placeholder="请输入物料生产时间" />
        </el-form-item>
        <el-form-item label="物料等级" prop="materialGrade">
          <el-input v-model="form.materialGrade" placeholder="请输入物料等级" />
        </el-form-item>
        <el-form-item label="物料有效期" prop="validityPeriod">
          <el-input v-model="form.validityPeriod" placeholder="请输入物料有效期" />
        </el-form-item>
        <el-form-item label="物料有效期单位" prop="validityUnit">
          <el-input v-model="form.validityUnit" placeholder="请输入物料有效期单位" />
        </el-form-item>
        <el-form-item label="物料暴露有效期" prop="exposureTime">
          <el-input v-model="form.exposureTime" placeholder="请输入物料暴露有效期" />
        </el-form-item>
        <el-form-item label="物料暴露有效期单位" prop="exposureUnit">
          <el-input v-model="form.exposureUnit" placeholder="请输入物料暴露有效期单位" />
        </el-form-item>
        <el-form-item label="收料人员工号编码" prop="recipientCode">
          <el-input v-model="form.recipientCode" placeholder="请输入收料人员工号编码" />
        </el-form-item>
        <el-form-item label="绑定操作的Port口ID" prop="portId">
          <el-input v-model="form.portId" placeholder="请输入绑定操作的Port口ID" />
        </el-form-item>
        <el-form-item label="区分待检料和已检料" prop="isQualityCheck">
          <el-input v-model="form.isQualityCheck" placeholder="请输入区分待检料和已检料" />
        </el-form-item>
        <el-form-item label="需要截料的数量" prop="lockNum">
          <el-input v-model="form.lockNum" placeholder="请输入需要截料的数量" />
        </el-form-item>
        <el-form-item label="为空表示该物料未被使用，不为空表示物料已被该单号占用" prop="usedByCode">
          <el-input v-model="form.usedByCode" placeholder="请输入为空表示该物料未被使用，不为空表示物料已被该单号占用" />
        </el-form-item>
        <el-form-item label="厂别(库区划分)" prop="plantCode">
          <el-input v-model="form.plantCode" placeholder="请输入厂别(库区划分)" />
        </el-form-item>
        <el-form-item label="是否截料" prop="isCutMaterial">
          <el-input v-model="form.isCutMaterial" placeholder="请输入是否截料" />
        </el-form-item>
        <el-form-item label="乐观锁" prop="version">
          <el-input v-model="form.version" placeholder="请输入乐观锁" />
        </el-form-item>
        <el-form-item label="入库标签栏位" prop="bin">
          <el-input v-model="form.bin" placeholder="请输入入库标签栏位" />
        </el-form-item>
        <el-form-item label="是否入库前之前截留" prop="isQualityCut">
          <el-input v-model="form.isQualityCut" placeholder="请输入是否入库前之前截留" />
        </el-form-item>
        <el-form-item label="用途主体编码" prop="usedByBodyCode">
          <el-input v-model="form.usedByBodyCode" placeholder="请输入用途主体编码" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Inventory">
import { listInventory, getInventory, delInventory, addInventory, updateInventory } from "@/api/work/inventory"
// import useUserStore from '@/store/modules/user'
// const userStore = useUserStore()
const { proxy } = getCurrentInstance()

const inventoryList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    materialBatch: null,
    materialCode: null,
    containerId: null,
    reelId: null,
    portId: null,
    isQualityCheck: null,
    qualityCheckType: null,
    lockNum: null,
    borrowStatus: null,
    usedByCode: null,
    warehouseType: null,
    plantCode: null,
    isCutMaterial: null,
    version: null,
    bin: null,
    isQualityCut: null,
    usedByBodyCode: null
  },
  rules: {
    lockNum: [
      { required: true, message: "需要截料的数量不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询库存列表 */
function getList() {
  loading.value = true
  listInventory(queryParams.value).then(res => {
    if (res.errCode === '1') {
      inventoryList.value = res.data.page.records
      total.value = res.data.page.total
      loading.value = false
    }
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    inventorId: null,
    materialBatch: null,
    materialCode: null,
    containerId: null,
    reelId: null,
    inventoryQty: null,
    inboundTime: null,
    compartmentCode: null,
    frozenState: null,
    note: null,
    status: null,
    productionDate: null,
    cavityNo: null,
    revision: null,
    createdBy: null,
    updateBy: null,
    updateTime: null,
    createTime: null,
    corporation: null,
    materialName: null,
    isUrgent: null,
    supplierCode: null,
    receivingTime: null,
    dateCode: null,
    materialGrade: null,
    validityPeriod: null,
    validityUnit: null,
    exposureTime: null,
    exposureUnit: null,
    recipientCode: null,
    portId: null,
    isQualityCheck: null,
    qualityCheckType: null,
    lockNum: null,
    borrowStatus: null,
    usedByCode: null,
    warehouseType: null,
    plantCode: null,
    isCutMaterial: null,
    version: null,
    bin: null,
    isQualityCut: null,
    usedByBodyCode: null
  }
  proxy.resetForm("inventoryRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.inventorId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加库存"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _inventorId = row.inventorId || ids.value
  getInventory(_inventorId).then(res => {
    form.value = res.data.inventory
    open.value = true
    title.value = "修改库存"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["inventoryRef"].validate(valid => {
    if (valid) {
      if (form.value.inventorId != null) {
        updateInventory(form.value).then(res => {
          if (res.errCode === '1') {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getList()
          } else {
            proxy.$modal.msgError(res.msg)
          }
        })
      } else {
        addInventory(form.value).then(res => {
          if (res.errCode === '1') {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          } else {
            proxy.$modal.msgError(res.msg)
          }
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _inventorIds = row.inventorId || ids.value
  proxy.$modal.confirm('是否确认删除库存编号为"' + _inventorIds + '"的数据项？').then(function() {
    return delInventory(_inventorIds)
  }).then(res => {
    if (res.errCode === '1') {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    } else {
      proxy.$modal.msgError(res.msg)
    }
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('api/inventory/export', {
    ...queryParams.value
  }, `inventory数据_${new Date().getTime()}.xlsx`)
}

getList()

// 下载模板
const downloadTemplate = async() => {
  proxy.download('/api/inventory/downloadTemplate', {}, `inventory模板_${new Date().getTime()}.xlsx`)
}
// 上传成功后的回调
const handleUploadSuccess = (res) => {
  getList()
}
// 部分成功回调
const handleUploadWarning = (res) => {
  getList()
}
</script>
